﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using Twitter;

namespace Twitter.membership
{
    public class RealTweetAppMembership : MembershipProvider
    {
        public override string ApplicationName
        {
            get
            {
                return "RealTweetApp";
            }
            set
            {
                
            }
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
                 TwitterEntitiesModelContainer context = new TwitterEntitiesModelContainer();//)
                  Member member = (from m in context.MemberJeu
                                   where m.login ==username
                                   && m.password == oldPassword
                                       select m).FirstOrDefault<Member>();
                 if(member == null)
                     return false;
                 member.password = newPassword;
                 context.MemberJeu.ApplyCurrentValues(member);
                 context.SaveChanges();
                 return true;
           
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            return true;
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
          //  using (
            TwitterEntitiesModelContainer context = new TwitterEntitiesModelContainer();//)
            {
                Member member = new Member();
                member.name = username;
                member.password = password;
                member.login = username;
                context.MemberJeu.AddObject(member);
                context.SaveChanges();
            }
            
            status = MembershipCreateStatus.Success;
            return GetUser(username, true);
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            using (TwitterEntitiesModelContainer context = new TwitterEntitiesModelContainer())
            {
                Member member = (from m in context.MemberJeu
                                   where m.login == username
                                   select m
                                 ).FirstOrDefault<Member>();

                 if(member == null)
                     return false;
                 context.MemberJeu.DeleteObject(member);
                 context.SaveChanges();
                 return true;

            }
        }

        public override bool EnablePasswordReset
        {
            get { return true; }
        }

        public override bool EnablePasswordRetrieval
        {
            get { return false; }
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            totalRecords = 0;
            return null;
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            MembershipUserCollection collection = new MembershipUserCollection();
            if (GetUser(usernameToMatch, true) != null)
            {
                collection.Add(GetUser(usernameToMatch, true));
                totalRecords = 1;
            }
            else
                totalRecords = 0;
            return collection;

        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            using (TwitterEntitiesModelContainer context = new TwitterEntitiesModelContainer())
            {
                MembershipUserCollection collection = new MembershipUserCollection();

                IEnumerable<Member> members = (from m in context.MemberJeu
                                  select m
                                 ).ToList<Member>();
                if (members == null)
                {
                    totalRecords = 0;
                    return collection;
                
                }

                foreach(Member member in members)
                {
                    MembershipUser mu = new MembershipUser(member.name,
                                              member.login,
                                              member.name,
                                              "default@RealTweetApp.com",
                                              member.name,
                                              member.name,
                                              true,
                                              false,
                                              new DateTime(),
                                              new DateTime(),
                                              new DateTime(),
                                              new DateTime(),
                                              new DateTime());
                    collection.Add(mu);
                }
                totalRecords = collection.Count;
                return collection;
            
            }
        }

        public override int GetNumberOfUsersOnline()
        {
            int c = 0;
            return GetAllUsers(0, 0, out c).Count;
        }

        public override string GetPassword(string username, string answer)
        {
            using (TwitterEntitiesModelContainer context = new TwitterEntitiesModelContainer())
            {
                Member member = (from m in context.MemberJeu
                                 where m.login == username
                                 select m
                                 ).FirstOrDefault<Member>();

                if (member == null)
                    return "";


                return member.password;
            }
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            using (TwitterEntitiesModelContainer context = new TwitterEntitiesModelContainer())
            {
                Member member = (from m in context.MemberJeu
                                 where m.login == username
                                 select m
                                 ).FirstOrDefault<Member>();


                if (member == null)
                    return null;


                MembershipUser mu = new MembershipUser("RealTweetApp",
                                              member.login,
                                              member.name,
                                              "default@RealTweetApp.com",
                                              member.name,
                                              member.name,
                                              true,
                                              false,
                                              new DateTime(),
                                              new DateTime(),
                                              new DateTime(),
                                              new DateTime(),
                                              new DateTime());

                return mu;
            }
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            using (TwitterEntitiesModelContainer context = new TwitterEntitiesModelContainer())
            {
                Member member = (from m in context.MemberJeu
                                 where m.login == providerUserKey.ToString()
                                 select m
                                 ).FirstOrDefault<Member>();


                if (member == null)
                    return null;


                MembershipUser mu = new MembershipUser(member.name,
                                              member.login,
                                              member.name,
                                              "default@RealTweetApp.com",
                                              member.name,
                                              member.name,
                                              true,
                                              false,
                                              new DateTime(),
                                              new DateTime(),
                                              new DateTime(),
                                              new DateTime(),
                                              new DateTime());

                return mu;
            }
        }

        public override string GetUserNameByEmail(string email)
        {
            return "";
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { return 0; }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { return 0; }
        }

        public override int MinRequiredPasswordLength
        {
            get { return 0; }
        }

        public override int PasswordAttemptWindow
        {
            get { return 0; }
        }

       
        public override bool RequiresQuestionAndAnswer
        {
            get { return false; }
        }

        public override bool RequiresUniqueEmail
        {
            get { return false; }
        }

        public override string ResetPassword(string username, string answer)
        {
            return "password";
        }

        public override bool UnlockUser(string userName)
        {
            return true;
        }

        public override void UpdateUser(MembershipUser user)
        {
            
        }

        public override bool ValidateUser(string username, string password)
        {
            using (TwitterEntitiesModelContainer context = new TwitterEntitiesModelContainer())
            {
                Member member = (from m in context.MemberJeu
                                 where m.login == username
                                 && m.password == password
                                 select m
                                 ).FirstOrDefault<Member>();

                if (member == null)
                    return false;
                return true;
            }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get {
                MembershipPasswordFormat mp = new MembershipPasswordFormat();
                return mp;
              
            }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { return "*+"; }
        }
    }
}